/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import bean.EncounterBean;
import bean.ProcedureRequestBean;
import java.io.UnsupportedEncodingException;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hl7.fhir.dstu3.model.Encounter;
import org.hl7.fhir.dstu3.model.Patient;
import util.FhirContextContstant;
import util.FormatUtil;
import util.HttpUtil;
import util.MakeTempalteStringUtil;

/**
 *
 * @author wudihua
 */
public class FhirProcedureRequestGui extends javax.swing.JFrame {

    private static final Logger logger = LogManager.getLogger(FhirProcedureRequestGui.class);

    
    private ProcedureRequestBean procedureRequestBean;
 
    /**
     * Creates new form FhirPatientGui
     */
    public FhirProcedureRequestGui() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel_requestId = new javax.swing.JLabel();
        jLabel_status = new javax.swing.JLabel();
        jTextField_requestId = new javax.swing.JTextField();
        jLabel_intend = new javax.swing.JLabel();
        jLabel_inspectCode = new javax.swing.JLabel();
        jTextField_inspectCode = new javax.swing.JTextField();
        jLabel_inspectName = new javax.swing.JLabel();
        jTextField_inspectName = new javax.swing.JTextField();
        jLabel_patientName = new javax.swing.JLabel();
        jTextField_patientName = new javax.swing.JTextField();
        jLabel_occurDateTime = new javax.swing.JLabel();
        jLabel_authoredOn = new javax.swing.JLabel();
        jTextField_authoredOn = new javax.swing.JTextField();
        jLabel_reasonCode = new javax.swing.JLabel();
        jTextField_reasonCode = new javax.swing.JTextField();
        jLabel_body = new javax.swing.JLabel();
        jTextField_body = new javax.swing.JTextField();
        jLabel_applyDoctor = new javax.swing.JLabel();
        jTextField_applyDoctor = new javax.swing.JTextField();
        jButton_clear = new javax.swing.JButton();
        jComboBox_status = new javax.swing.JComboBox<>();
        jComboBox_intend = new javax.swing.JComboBox<>();
        jTextField_occurDateTime = new javax.swing.JTextField();
        jLabel_bodyCode = new javax.swing.JLabel();
        jTextField_bodyCode = new javax.swing.JTextField();
        jLabel_refPatient = new javax.swing.JLabel();
        jTextField_refPatient = new javax.swing.JTextField();
        jLabel_refEncounter = new javax.swing.JLabel();
        jTextField_refEncounter = new javax.swing.JTextField();
        jLabel_refDoctor = new javax.swing.JLabel();
        jTextField_refDoctor = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jButton_create = new javax.swing.JButton();
        jButton_update = new javax.swing.JButton();
        jButton_query = new javax.swing.JButton();
        jButton_queryHistory = new javax.swing.JButton();
        jButton_delete = new javax.swing.JButton();
        jPanel_showXml = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea_showXml = new javax.swing.JTextArea();
        jLabel_url = new javax.swing.JLabel();
        jTextField_url = new javax.swing.JTextField();
        jComboBox_dataType = new javax.swing.JComboBox<>();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("检查申请");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel_requestId.setText("资源ID：");

        jLabel_status.setText("申请状态：");

        jTextField_requestId.setToolTipText("");

        jLabel_intend.setText("申请目的：");

        jLabel_inspectCode.setText("检查项目编码：");

        jTextField_inspectCode.setText("80859-2");

        jLabel_inspectName.setText("检查项目：");

        jTextField_inspectName.setText("心脏惭色多普勒超声");

        jLabel_patientName.setText("患者姓名：");

        jLabel_occurDateTime.setText("预定检查时间：");

        jLabel_authoredOn.setText("申请时间：");

        jTextField_authoredOn.setText("2018-05-01T10:33:27+07:00");

        jLabel_reasonCode.setText("申请原因：");

        jTextField_reasonCode.setText("患者心悸");

        jLabel_body.setText("检查部位：");

        jTextField_body.setText("心脏");

        jLabel_applyDoctor.setText("申请医生：");

        jTextField_applyDoctor.setText("李小青");
        jTextField_applyDoctor.setToolTipText("");

        jButton_clear.setText("清空");
        jButton_clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_clearActionPerformed(evt);
            }
        });

        jComboBox_status.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "active", "draft", "suspended", "cancelled", "completed", "entered-in-error", "unknown" }));

        jComboBox_intend.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "original-order", "proposal", "plan", "order", "reflex-order", "filler-order", "instance-order", "option" }));

        jTextField_occurDateTime.setText("2018-05-02T09:00:00+07:00");

        jLabel_bodyCode.setText("检查部位编码：");

        jTextField_bodyCode.setText("80891009");

        jLabel_refPatient.setText("关联患者ID：");

        jTextField_refPatient.setText("1");

        jLabel_refEncounter.setText("关联就诊：");

        jTextField_refEncounter.setText("19");

        jLabel_refDoctor.setText("关联医生：");

        jTextField_refDoctor.setText("doc1");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel_bodyCode)
                        .addGap(49, 49, 49)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField_bodyCode, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTextField_body, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField_reasonCode, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel_status)
                                .addComponent(jLabel_requestId)
                                .addComponent(jLabel_intend))
                            .addGap(74, 74, 74)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextField_requestId)
                                .addComponent(jComboBox_status, 0, 177, Short.MAX_VALUE)
                                .addComponent(jComboBox_intend, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel_inspectCode)
                                .addComponent(jLabel_inspectName)
                                .addComponent(jLabel_patientName)
                                .addComponent(jLabel_occurDateTime)
                                .addComponent(jLabel_authoredOn)
                                .addComponent(jLabel_applyDoctor)
                                .addComponent(jLabel_refPatient)
                                .addComponent(jLabel_refEncounter)
                                .addComponent(jLabel_refDoctor))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTextField_patientName, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jTextField_occurDateTime, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                                .addComponent(jTextField_authoredOn, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jTextField_applyDoctor, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(1, 1, 1)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jTextField_inspectName)
                                        .addComponent(jTextField_inspectCode)))
                                .addComponent(jTextField_refPatient)
                                .addComponent(jTextField_refEncounter)
                                .addComponent(jTextField_refDoctor))))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton_clear)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel_reasonCode)
                                .addComponent(jLabel_body))
                            .addGap(333, 333, 333))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_requestId)
                    .addComponent(jTextField_requestId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_status)
                    .addComponent(jComboBox_status, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_intend)
                    .addComponent(jComboBox_intend, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_inspectCode)
                    .addComponent(jTextField_inspectCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_inspectName)
                    .addComponent(jTextField_inspectName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_refPatient)
                    .addComponent(jTextField_refPatient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_patientName)
                    .addComponent(jTextField_patientName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_refEncounter)
                    .addComponent(jTextField_refEncounter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_occurDateTime)
                    .addComponent(jTextField_occurDateTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_authoredOn)
                    .addComponent(jTextField_authoredOn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_refDoctor)
                    .addComponent(jTextField_refDoctor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField_applyDoctor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_applyDoctor))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField_reasonCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_reasonCode))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel_bodyCode)
                        .addGap(14, 14, 14)
                        .addComponent(jLabel_body))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jTextField_bodyCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField_body, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(72, 72, 72)
                .addComponent(jButton_clear)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jButton_create.setText("创建");
        jButton_create.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_createActionPerformed(evt);
            }
        });

        jButton_update.setText("更新");
        jButton_update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_updateActionPerformed(evt);
            }
        });

        jButton_query.setText("检索");
        jButton_query.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_queryActionPerformed(evt);
            }
        });

        jButton_queryHistory.setText("获取历史信息");
        jButton_queryHistory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_queryHistoryActionPerformed(evt);
            }
        });

        jButton_delete.setText("删除");
        jButton_delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_deleteActionPerformed(evt);
            }
        });

        jPanel_showXml.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jTextArea_showXml.setColumns(20);
        jTextArea_showXml.setRows(5);
        jScrollPane2.setViewportView(jTextArea_showXml);

        javax.swing.GroupLayout jPanel_showXmlLayout = new javax.swing.GroupLayout(jPanel_showXml);
        jPanel_showXml.setLayout(jPanel_showXmlLayout);
        jPanel_showXmlLayout.setHorizontalGroup(
            jPanel_showXmlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_showXmlLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel_showXmlLayout.setVerticalGroup(
            jPanel_showXmlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_showXmlLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2)
                .addContainerGap())
        );

        jLabel_url.setText("服务器URL：");

        jTextField_url.setText("http://36.33.216.237:9009/fhir/winningfhir/ProcedureRequest");

        jComboBox_dataType.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "xml", "json" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(61, 61, 61)
                                        .addComponent(jLabel7))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jButton_create)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton_query)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton_update)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton_queryHistory)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton_delete)))
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel_showXml, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel_url)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField_url, javax.swing.GroupLayout.PREFERRED_SIZE, 586, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jComboBox_dataType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_create)
                    .addComponent(jButton_query)
                    .addComponent(jButton_queryHistory)
                    .addComponent(jButton_delete)
                    .addComponent(jButton_update))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_url)
                    .addComponent(jTextField_url, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox_dataType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel_showXml, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    //创建
    private void jButton_createActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_createActionPerformed
        //关联患者和就诊
        ProcedureRequestBean procedureRequestBean = relatePatientAndEncounterBean(evt);

        String url = this.jTextField_url.getText();
        //获取发送数据的类型 xml/json
        String datatType = this.jComboBox_dataType.getSelectedItem().toString();
        String procedureRequestString = "";
        String result = "";
        
        if ("xml".equals(datatType)) {
            procedureRequestString = MakeTempalteStringUtil.makeProcedureRequestString(procedureRequestBean, "/template/procedureRequestTemplate.xml");
            procedureRequestString = FormatUtil.formatXml(procedureRequestString);
            logger.info("创建 procedureRequest xml: {}", procedureRequestString);
            result = HttpUtil.sendPost(url, procedureRequestString, HttpUtil.ContentType_xml);
            result = FormatUtil.formatXml(result);
        } else if ("json".equals(datatType)){
            procedureRequestString = MakeTempalteStringUtil.makeProcedureRequestString(procedureRequestBean, "/template/json/procedureRequestTemplate.json");
            logger.info("创建 procedureRequest json: {}", procedureRequestString);
            result = HttpUtil.sendPost(url, procedureRequestString, HttpUtil.ContentType_json);
            result = FormatUtil.formatJson(result);
        }
        
//        String procedureRequestXml = MakeXmlUtil.makeProcedureRequestXml(procedureRequestBean);
//        procedureRequestXml = FormatXmlUtil.formatXml(procedureRequestXml);
//        logger.info("创建 procedureRequestXml: {}", procedureRequestXml);
//        
//        String url = this.jTextField_url.getText();
//        String result = HttpUtil.sendPost(url, procedureRequestXml, HttpUtil.ContentType_xml);
        //logger.info("{} result:{}",url,result);
        this.jTextArea_showXml.setText(result);
        logger.info("创建成功");
        // 创建
       
    }//GEN-LAST:event_jButton_createActionPerformed
    //关联患者和就诊
    private ProcedureRequestBean relatePatientAndEncounterBean(java.awt.event.ActionEvent evt) {
        String url = this.jTextField_url.getText();
        String datatType = this.jComboBox_dataType.getSelectedItem().toString();
        procedureRequestBean = new ProcedureRequestBean();
        
        //首先查询关联的id是否存在
        //关联患者 id
        String patientId = this.jTextField_refPatient.getText();
        procedureRequestBean.setRefPatient(patientId);
        //关联就诊 id
        String encounterId = this.jTextField_refEncounter.getText();
        procedureRequestBean.setRefencounter(encounterId);
        
        String baseUrl = url.substring(0, url.indexOf("ProcedureRequest"));
        String queryPatientUrl = baseUrl + "/Patient/" + patientId;
        String queryEncounterUrl = baseUrl + "/Encounter/" + encounterId;
        String queryPatientResult = null;
        String queryEncounterResult = null;
        
        if ("xml".equals(datatType)) {
            queryPatientResult = HttpUtil.sendGet(queryPatientUrl,HttpUtil.ContentType_xml);
            queryEncounterResult = HttpUtil.sendGet(queryEncounterUrl,HttpUtil.ContentType_xml);
            if (queryPatientResult.contains("OperationOutcome")||queryPatientResult.length()<100) { //没对应患者id 报错
                this.jTextArea_showXml.setText(queryPatientResult);
                //结束
            }else if (queryEncounterResult.contains("OperationOutcome")||queryEncounterResult.length()<100) {//没对应就诊id 报错
                this.jTextArea_showXml.setText(queryEncounterResult);
                //结束
            }else{  //对应患者id 对应就诊id
        
                Patient patient = FhirContextContstant.getXMLParse().parseResource(Patient.class, queryPatientResult);
                procedureRequestBean.setRelationPatient(patient.getNameFirstRep().getText());
                //Encounter encounter =  FhirContextContstant.getXMLParse().parseResource(Encounter.class, queryEncounterResult);
                //执行创建
                return setProcedureRequestBean(evt, procedureRequestBean);
            }
        }else if ("json".equals(datatType)){
            queryPatientResult = HttpUtil.sendGet(queryPatientUrl,HttpUtil.ContentType_json);
            if (queryPatientResult.contains("OperationOutcome")||queryPatientResult.length()<100) {//没对应患者id 报错
                this.jTextArea_showXml.setText(queryPatientResult);
                //结束
            }else if (queryEncounterResult.contains("OperationOutcome")||queryEncounterResult.length()<100) {//没对应就诊id 报错
                this.jTextArea_showXml.setText(queryEncounterResult);
                //结束
            }else{
                Patient patient = FhirContextContstant.getJsonParse().parseResource(Patient.class, queryPatientResult);
                procedureRequestBean.setRelationPatient(patient.getNameFirstRep().getText());
                //执行创建
                return setProcedureRequestBean(evt, procedureRequestBean);
            }
        } 
        return null;
    }
    
    //赋值
    private ProcedureRequestBean setProcedureRequestBean(java.awt.event.ActionEvent evt, ProcedureRequestBean procedureRequestBean) {
        procedureRequestBean.setPrId("001");
        procedureRequestBean.setStatus(this.jComboBox_status.getSelectedItem().toString());
        procedureRequestBean.setIntent(this.jComboBox_intend.getSelectedItem().toString());
        
        procedureRequestBean.setInspectCode(this.jTextField_inspectCode.getText());
        procedureRequestBean.setInspectName(this.jTextField_inspectName.getText());
        
//        procedureRequestBean.setRefPatient(this.jTextField_refPatient.getText());
//        procedureRequestBean.setRelationPatient(this.jTextField_patientName.getText());
        
//        procedureRequestBean.setRefencounter(this.jTextField_refEncounter.getText());
        
        procedureRequestBean.setOccurrenceDateTime(this.jTextField_occurDateTime.getText());
        procedureRequestBean.setAuthoredOn(this.jTextField_authoredOn.getText());
        
        procedureRequestBean.setRefDcotor(this.jTextField_refDoctor.getText());
        procedureRequestBean.setApplyDoctor(this.jTextField_applyDoctor.getText());
        
        procedureRequestBean.setReasonCode(this.jTextField_reasonCode.getText());
        
        procedureRequestBean.setApplyCode(this.jTextField_bodyCode.getText());
        procedureRequestBean.setApplyPosition(this.jTextField_body.getText());

        return procedureRequestBean;
    }
    //关闭窗口
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        this.setDefaultCloseOperation(FhirProcedureRequestGui.DISPOSE_ON_CLOSE);
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosing
    //清除
    private void jButton_clearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_clearActionPerformed
        this.jTextField_inspectCode.setText("");
        this.jTextField_inspectName.setText("");
        this.jTextField_refPatient.setText("");
        this.jTextField_patientName.setText("");
        this.jTextField_refEncounter.setText("");
        this.jTextField_occurDateTime.setText("");
        this.jTextField_authoredOn.setText("");
        this.jTextField_refDoctor.setText("");
        this.jTextField_applyDoctor.setText("");
        this.jTextField_reasonCode.setText("");
         this.jTextField_bodyCode.setText("");
        this.jTextField_body.setText("");

        //        this..setText("");        
        // 清空
    }//GEN-LAST:event_jButton_clearActionPerformed
    //查询
    private void jButton_queryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_queryActionPerformed
//        String url = this.jTextField_url.getText();
//        String procedureRequestId = this.jTextField_requestId.getText();
//        String patientName = this.jTextField_patientName.getText();
//        String result = null;
//        if (!StringUtils.isEmpty(procedureRequestId)) {
//            url = url + "/" + procedureRequestId;
//            result = HttpUtil.sendGet(url);
//        } else if (!StringUtils.isEmpty(patientName)) {
//            try {   
//                patientName =  java.net.URLEncoder.encode(patientName,"utf-8");
//            } catch (UnsupportedEncodingException ex) {
//                java.util.logging.Logger.getLogger(FhirProcedureRequestGui.class.getName()).log(Level.SEVERE, null, ex);
//            }
//            url = url + "/" + patientName;
//            result = HttpUtil.sendGet(url);
//        }
//        if (!StringUtils.isEmpty(result)) {
//             result = FormatXmlUtil.formatXml(result);
//             this.jTextArea_showXml.setText(result);
//        }else{
//            this.jTextArea_showXml.setText("查询结果为空");
//        }
        String url = this.jTextField_url.getText();
        String result = null;
        String datatType = this.jComboBox_dataType.getSelectedItem().toString();
        if ("xml".equals(datatType)) {
            result = HttpUtil.sendGet(url,HttpUtil.ContentType_xml);
            result = FormatUtil.formatXml(result);
        } else if("json".equals(datatType)) {
            result = HttpUtil.sendGet(url,HttpUtil.ContentType_json);
            result = FormatUtil.formatJson(result);
        }
        //logger.info(" url={} result {} ",url, result);
        this.jTextArea_showXml.setText(result);

        //检索
    }//GEN-LAST:event_jButton_queryActionPerformed
    //历史查询
    private void jButton_queryHistoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_queryHistoryActionPerformed
          jButton_queryActionPerformed(evt);

//        String url = this.jTextField_url.getText();
//        String procedureRequestId = this.jTextField_requestId.getText();
//        String result = null;
//        if (!StringUtils.isEmpty(procedureRequestId)) {
//            url = url + "/" + procedureRequestId;
//            result = HttpUtil.sendGet(url);
//        }
//         if (!StringUtils.isEmpty(result)) {
//             result = FormatXmlUtil.formatXml(result);
//             this.jTextArea_showXml.setText(result);
//        }else{
//            this.jTextArea_showXml.setText("查询结果为空");
//        }
//       String url = this.jTextField_url.getText();
//       String result = HttpUtil.sendGet(url);
//       String datatType = this.jComboBox_dataType.getSelectedItem().toString();
//        
//        if (!StringUtils.isEmpty(result)) {
//            if ("xml".equals(datatType)) {
//                result = FormatXmlUtil.formatXml(result);
//            } else if("json".equals(datatType)) {
//                 result = FormatJsonUtil.formatJson(result);
//            }
//            logger.info("query url result {} ", result);
//             this.jTextArea_showXml.setText(result);
//        }else{
//            this.jTextArea_showXml.setText("查询结果为空");
//        }
      
        // 获取历史版本
    }//GEN-LAST:event_jButton_queryHistoryActionPerformed
    //删除
    private void jButton_deleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_deleteActionPerformed
        //jButton_queryActionPerformed(evt);

//        String url = this.jTextField_url.getText().trim();
//        String procedureRequestId = this.jTextField_requestId.getText().trim();
//
//        if (!StringUtils.isEmpty(procedureRequestId)) {
//            url = url + "/" + procedureRequestId;
//            HttpUtil.sendGet(url);
//            this.jTextArea_showXml.setText("删除成功");
//        }
        String url = this.jTextField_url.getText().trim();
        String datatType = this.jComboBox_dataType.getSelectedItem().toString();
        String result = null;
        if ("xml".equals(datatType)) {
            result = HttpUtil.sendDelete(url,HttpUtil.ContentType_xml);
            result = FormatUtil.formatXml(result);
        } else if("json".equals(datatType)) {
            result = HttpUtil.sendDelete(url,HttpUtil.ContentType_json);
             result = FormatUtil.formatJson(result);
        }
        //logger.info("delete url result {} ", result);
        this.jTextArea_showXml.setText(result);
        // 删除
    }//GEN-LAST:event_jButton_deleteActionPerformed
    //更新
    private void jButton_updateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_updateActionPerformed
//        procedureRequestBean = new ProcedureRequestBean();
//        procedureRequestBean.setPrId(this.jTextField_requestId.getText());
//        procedureRequestBean.setStatus(this.jComboBox_status.getSelectedItem().toString());
//        procedureRequestBean.setIntent(this.jComboBox_intend.getSelectedItem().toString());
//        
//        procedureRequestBean.setInspectCode(this.jTextField_inspectCode.getText());
//        procedureRequestBean.setInspectName(this.jTextField_inspectName.getText());
//        
//        procedureRequestBean.setRefPatient(this.jTextField_refPatient.getText());
//        procedureRequestBean.setRelationPatient(this.jTextField_patientName.getText());
//        
//        procedureRequestBean.setRefencounter(this.jTextField_refEncounter.getText());
//        
//        procedureRequestBean.setOccurrenceDateTime(this.jTextField_occurDateTime.getText());
//        procedureRequestBean.setAuthoredOn(this.jTextField_authoredOn.getText());
//        
//        procedureRequestBean.setRefDcotor(this.jTextField_refDoctor.getText());
//        procedureRequestBean.setApplyDoctor(this.jTextField_applyDoctor.getText());
//        
//        procedureRequestBean.setReasonCode(this.jTextField_reasonCode.getText());
//        
//        procedureRequestBean.setApplyCode(this.jTextField_bodyCode.getText());
//        procedureRequestBean.setApplyPosition(this.jTextField_body.getText());
        //关联患者和就诊
        ProcedureRequestBean procedureRequestBean = relatePatientAndEncounterBean(evt);
        procedureRequestBean.setPrId(this.jTextField_requestId.getText());
        String url = this.jTextField_url.getText();
        String procedureRequestString = "";
        String result = "";
        //获取发送数据的类型 xml/json
        String datatType = this.jComboBox_dataType.getSelectedItem().toString();
        if ("xml".equals(datatType)) {
           procedureRequestString = MakeTempalteStringUtil.makeProcedureRequestString(procedureRequestBean, "/template/procedureRequestTemplate.xml");
            procedureRequestString = FormatUtil.formatXml(procedureRequestString);
            logger.info("更新 procedureRequest xml: {}", procedureRequestString);
            result = HttpUtil.sendPut(url, procedureRequestString, HttpUtil.ContentType_xml);
            result = FormatUtil.formatXml(result);
        } else if ("json".equals(datatType)){
            procedureRequestString = MakeTempalteStringUtil.makeProcedureRequestString(procedureRequestBean, "/template/json/procedureRequestTemplate.json");
            logger.info("更新 procedureRequest json: {}", procedureRequestString);
            result = HttpUtil.sendPut(url, procedureRequestString, HttpUtil.ContentType_json);
            result = FormatUtil.formatJson(result);
        }
        this.jTextArea_showXml.setText(result);
        logger.info("更新成功");

        // 更新
    }//GEN-LAST:event_jButton_updateActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FhirProcedureRequestGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FhirProcedureRequestGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FhirProcedureRequestGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FhirProcedureRequestGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FhirProcedureRequestGui().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_clear;
    private javax.swing.JButton jButton_create;
    private javax.swing.JButton jButton_delete;
    private javax.swing.JButton jButton_query;
    private javax.swing.JButton jButton_queryHistory;
    private javax.swing.JButton jButton_update;
    private javax.swing.JComboBox<String> jComboBox_dataType;
    private javax.swing.JComboBox<String> jComboBox_intend;
    private javax.swing.JComboBox<String> jComboBox_status;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel_applyDoctor;
    private javax.swing.JLabel jLabel_authoredOn;
    private javax.swing.JLabel jLabel_body;
    private javax.swing.JLabel jLabel_bodyCode;
    private javax.swing.JLabel jLabel_inspectCode;
    private javax.swing.JLabel jLabel_inspectName;
    private javax.swing.JLabel jLabel_intend;
    private javax.swing.JLabel jLabel_occurDateTime;
    private javax.swing.JLabel jLabel_patientName;
    private javax.swing.JLabel jLabel_reasonCode;
    private javax.swing.JLabel jLabel_refDoctor;
    private javax.swing.JLabel jLabel_refEncounter;
    private javax.swing.JLabel jLabel_refPatient;
    private javax.swing.JLabel jLabel_requestId;
    private javax.swing.JLabel jLabel_status;
    private javax.swing.JLabel jLabel_url;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel_showXml;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea_showXml;
    private javax.swing.JTextField jTextField_applyDoctor;
    private javax.swing.JTextField jTextField_authoredOn;
    private javax.swing.JTextField jTextField_body;
    private javax.swing.JTextField jTextField_bodyCode;
    private javax.swing.JTextField jTextField_inspectCode;
    private javax.swing.JTextField jTextField_inspectName;
    private javax.swing.JTextField jTextField_occurDateTime;
    private javax.swing.JTextField jTextField_patientName;
    private javax.swing.JTextField jTextField_reasonCode;
    private javax.swing.JTextField jTextField_refDoctor;
    private javax.swing.JTextField jTextField_refEncounter;
    private javax.swing.JTextField jTextField_refPatient;
    private javax.swing.JTextField jTextField_requestId;
    private javax.swing.JTextField jTextField_url;
    // End of variables declaration//GEN-END:variables
}
